<?php
/**
 * Created by PhpStorm.
 * User: silov
 * Date: 15/6/24
 * Time: 15:13
 */
namespace Sys\Model;
use Think\Cache\Driver\Redis;
use Think\Model;
class AdminMenuModel extends Model {
    protected $tableName = "admin_menu";

    /**
     * @var string Api token redis key Pre_Uhash
     */
    private $tokenRedis = "AdminViewLevelList";

    /**
     * 获取侧边栏的目录列表
     * @return array
     */
    public function getForLeftTab()
    {
        $con = [
            'level' => 2,
            'status'    => 1
        ];

        $order = [
            'order_index'   => 'ASC',
            'id'       => 'ASC'
        ];
        $tmpList = $this->where($con)->order($order)->select();

        $menuList = [];

        if (!empty($tmpList)) {
            foreach ($tmpList as $group) {
                $sonCon = [
                    'level' => 3,
                    'parent_id' => $group['id']
                ];
                $tmpSons = $this->where($sonCon)->order($order)->select();

                $sonList = [];
                if (!empty($tmpSons)) {
                    foreach ($tmpSons as $sonPoint) {
                        $sonList[] = [
                            'id'    => $sonPoint['level_sign'],
                            'href'  => $sonPoint['uri'],
                            'label' => $sonPoint['title'],
                            'class' => 'fa-gavel'
                        ];
                    }
                }

                $thisMenu = [
                    'label'     => $group['title'],
                    'class'     => 'fa-list'
                ];

                if ($group['type'] == 1) {
                    $thisMenu['labels'] = $sonList;
                    $thisMenu['level']  = 2;
                } else {
                    $thisMenu['level']  = 1;
                    $thisMenu['href']   = $group['uri'];
                }

                $menuList[] = $thisMenu;
            }
        }
        return $menuList;
    }

    /**
     * 获取所有目录信息
     * @return array
     */
    public function getForManager()
    {
        $con = [
            'level' => 2,
        ];

        $order = [
            'status'        => 'DESC',
            'order_index'   => 'ASC',
            'id'       => 'ASC'
        ];
        $tmpList = $this->where($con)->order($order)->select();

        $menuList = [];

        if (!empty($tmpList)) {
            foreach ($tmpList as $group) {
                $sonCon = [
                    'level' => 3,
                    'parent_id' => $group['id']
                ];
                $tmpSons = $this->where($sonCon)->order($order)->select();

                $sonList = [];
                if (!empty($tmpSons)) {
                    foreach ($tmpSons as $sonPoint) {
                        $sonList[] = [
                            'id'            => $sonPoint['id'],
                            'level_sign'    => $sonPoint['level_sign'],
                            'uri'           => $sonPoint['uri'],
                            'title'         => $sonPoint['title'],
                            'order_index'   => $sonPoint['order_index'],
                            'level'         => 3,
                            'created'       => $sonPoint['created']
                        ];
                    }
                }

                $thisMenu = [
                    'id'        => $group['id'],
                    'title'     => $group['title'],
                    'level_sign'    => $group['level_sign'],
                    'order_index'   => $group['order_index'],
                    'created'       => $group['created']
                ];

                if ($group['type'] == 1) {
                    $thisMenu['son_list'] = $sonList;
                    $thisMenu['level']  = 2;
                } else {
                    $thisMenu['level']  = 1;
                    $thisMenu['uri']   = $group['uri'];
                }

                $menuList[] = $thisMenu;
            }
        }
        return $menuList;
    }

    /**
     * 根据父级目录id获取子目录总数
     * @param $parentId
     * @return int
     */
    public function getSonCountByParentId($parentId)
    {
        $con = [
            'parent_id' => intval($parentId),
            'status'    => 1
        ];

        return $this->where($con)->count();
    }

    /**
     * @param $parentId
     * @param $menuId
     * @param $move
     * @return bool
     */
    public function rebuildOrder($parentId, $menuId, $move)
    {
        $con = [
            'parent_id' => intval($parentId),
        ];

        $order = [
            'order_index'   => 'ASC',
            'id'       => 'ASC'
        ];
        $tmpList = $this->where($con)->order($order)->select();

        if (empty($tmpList)) {
            return false;
        }
        $oldOrder = [];
        $i = 1;
        foreach ($tmpList as $menu) {
            $oldOrder[$menu['id']] = $i;
            $i ++;
        }

        $i --;

        if( ($move == 'up' && $oldOrder[$menuId]==1) || ($move == 'down' && $oldOrder[$menuId]==$i) ) {
            return true;
        }

        if ($move == 'up') {
            $changeOrder = $oldOrder[$menuId] - 1;
        } else {
            $changeOrder = $oldOrder[$menuId] + 1;
        }
        foreach ($oldOrder as $mId => $orderIndex) {
            if ($menuId == $mId) {
                $data = [
                    'order_index'   => intval($changeOrder)
                ];
            } elseif ($orderIndex == $changeOrder) {
                $data = [
                    'order_index'   => $move == 'up' ? intval($orderIndex + 1) : intval($orderIndex - 1)
                ];
            } else {
                $data = [
                    'order_index'   => intval($orderIndex)
                ];
            }

            $this->where(['id' => $mId])->data($data)->save();
        }

        return true;
    }
}